<?php
include_once 'protoweb_controller.php';

/**--------------------------------------------------------------------------
 * Please, don't touch this file.
 *
 * If you need to custom this Class, you can edit subscribers.php
 * file overwrite and define new methods
 *---------------------------------------------------------------------------
 */

/**
 * @package Protoweb
 * @subpackage controller
 */
class Subscribers_protoweb extends Protoweb_controller {

  public function __construct() {
    parent::__construct();
  } // end of method __construct()



  public function edit($id=0) {
    $data["subscriber"] = new Subscriber($id);

    if ($this->input->post("posting") == 1)
      $data = $this->edit_save($data["subscriber"]->id);

    // To one relationships
        
    $recipients = new Recipient();
    $data["recipients"] = $recipients->all_to_asoc_array("id","email");
    $data["recipients_selected"] = $data["subscriber"]->recipient->id;
        
    $catalogs = new Catalog();
    $data["catalogs"] = $catalogs->all_to_asoc_array("id","name");
    $data["catalogs_selected"] = $data["subscriber"]->catalog->id;
    
    // To many relationships
    
    $this->smarty_parser->view('subscriber_edit.html', $data);
  } // end of method edit($id=0)


  private function edit_save($id) {
    $data["error"] = "";
    $data["subscriber"] = new Subscriber($id);

          // Assign value to field. If value is empty, we will assign NULL
      $data["subscriber"]->subscribed = $this->input->post("subscribed");
      if ($data["subscriber"]->subscribed == "") $data["subscriber"]->subscribed = NULL;
      
      // files for upload
      /*
      if ($data["subscriber"]->subscribed->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(subscribed)) $data["subscriber"]->subscribed = $fileupload.clean_file_name(subscribed);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["subscriber"]->unsubscribed = $this->input->post("unsubscribed");
      if ($data["subscriber"]->unsubscribed == "") $data["subscriber"]->unsubscribed = NULL;
      
      // files for upload
      /*
      if ($data["subscriber"]->unsubscribed->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(unsubscribed)) $data["subscriber"]->unsubscribed = $fileupload.clean_file_name(unsubscribed);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
    
    //saving attributes
    if(! $data["subscriber"]->save())
      $data["error"] .= $data["subscriber"]->error->string;

    //saving to one relationships
    
    $recipient = new Recipient($this->input->post("recipient"));
    if (! $data["subscriber"]->save(array($recipient)))
      $data["error"] .= $data["subscriber"]->error->string;
    
    $catalog = new Catalog($this->input->post("catalog"));
    if (! $data["subscriber"]->save(array($catalog)))
      $data["error"] .= $data["subscriber"]->error->string;
    
    //to many relationships
    
    redirect(base_url()."subscribers/edit/".$data["subscriber"]->id);
  } // end of method edit_save($id)


  public function delete($id) {
    $data["error"] = "";
    $data["subscriber"] = new Subscriber($id);

    //delete
    if(! $data["subscriber"]->delete())
      $data["error"] .= $data["subscriber"]->error->string;

    redirect(base_url()."subscribers/edit/");
  } // end of method delete($id)

    
} // class Subscribers_protoweb

/* End of file subscribers_protoweb.php */
/* Location: ./system/application/controllers/subscribers_protoweb.php */
